<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/dataAreaFunction.php");

$errMsg = "";
if(sxREQ("reqtype") == "save"){
	$errMsg .= dataAreaIns();
	$dataArea = sxREQUEST("data_area");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$doc_name = sxREQUEST("doc_name");
}else{
	$dataArea = "";
	$description = "";
	$note = "";
	$doc_name = "";
}

//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>false);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objDA =  document.getElementById("data_area");
			var objDe =  document.getElementById("description");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objDe.value == ""){
				errMsg += "<?php echo _t("missing description")?><br>";
				objDe.focus();
			}
			if(objDA.value == ""){
				errMsg += "<?php echo _t("missing code")?><br>";
				objDA.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save new data area");
		}			
	}
</script> 
<?php
//output comandi
$aCmd = array("insDA"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Write data"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0, false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0)
										)
					);
echo(commandsHTML($aCmd));	
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"dataAreaIns.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Data-Area")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Code")."(*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"data_area\" id=\"data_area\" maxlength=\"4\" value=\"$dataArea\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")."(*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Document")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"doc_name\" id=\"doc_name\" maxlength=\"80\" size=\"32\" value=\"".sxHE($doc_name)."\">&nbsp;&nbsp;<input class=\"inpt01\" type=\"file\" name=\"document\" id=\"document\"></td>" .
		"</tr>\n");		
echo("</table>");
echo("</form>");
echo(commandsHTML($aCmd));
echo(footerHTML());
exit();

function dataAreaIns(){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST["data_area"] = strtolower(trim($_REQUEST["data_area"]));
	if(ereg("[^a-z0-9]",$_REQUEST["data_area"]) or strlen($_REQUEST["data_area"]) != 4)
		$errMsg .= _t("prefix must be 4 char long").".<br>";
	if(trim($_REQUEST["description"]) == '')
		$errMsg .= _t("Description missing.")."<br>";
	//check existence
	$sSQL = "SELECT count(*) as numda FROM data_area WHERE data_area='".$_REQUEST["data_area"]."'";
	$numEntity = 0;
	$objSDAO->query($sSQL);
	$row = $objSDAO->fetch();
	if($row["numda"] != 0)
		$errMsg .= _t("Data area existing");
	if($errMsg != ""){	
		return $errMsg;
	}	
	$sSQL = "INSERT INTO " .
			"data_area " .
			"(data_area, " .
			"description, " .
			"note)" .
			"VALUES('".addslashes(sxGPC(strtolower($_REQUEST["data_area"])))."', " .
			"'".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
			"'".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
			")";	
	
	$objSDAO->exec($sSQL);
	return '';
}

